@attribute [Route(PageUrls.ResetPassword)]
@attribute [Route("{culture?}" + PageUrls.ResetPassword)]
@inherits AppPageBase

<AppPageData Title="@Localizer[nameof(AppStrings.ResetPasswordTitle)]"
             PageTitle="@Localizer[nameof(AppStrings.ResetPasswordPageTitle)]" />

<section>
    <BitStack HorizontalAlign="BitAlignment.Center">
        <BitStack HorizontalAlign="BitAlignment.Center" FillContent Class="max-width">
            <BitText Typography="BitTypography.H4" Gutter>@Localizer[nameof(AppStrings.ResetPasswordTitle)]</BitText>

            @if (isPasswordChanged is false)
            {
                <BitText Typography="BitTypography.Subtitle1" Align="BitTextAlign.Center">
                    @Localizer[nameof(AppStrings.ResetPasswordSubtitle)]
                    @Localizer[nameof(AppStrings.ResetPasswordMessage)]
                </BitText>
                <br />
                <EditForm Model="model" OnValidSubmit="WrapHandled(Submit)" novalidate>
                    <AppDataAnnotationsValidator />

                    <BitStack FillContent Gap="2rem">
                        @if (isTokenEntered is false)
                        {
                            <BitStack FillContent>
                                @if (showEmail && showPhone)
                                {
                                    <BitPivot Alignment="BitAlignment.Center" SelectedKey="@selectedKey" SelectedKeyChanged="OnSelectedKeyChanged">
                                        <BitPivotItem HeaderText="@Localizer[nameof(AppStrings.Email)]" Key="@EmailKey">
                                            <BitTextField @bind-Value="model.Email"
                                                          AutoFocus TabIndex="1"
                                                          Type="BitInputType.Email"
                                                          Label="@Localizer[nameof(AppStrings.Email)]"
                                                          IsEnabled="string.IsNullOrEmpty(EmailQueryString)"
                                                          Placeholder="@Localizer[nameof(AppStrings.EmailPlaceholder)]" />
                                            <ValidationMessage For="@(() => model.Email)" />
                                        </BitPivotItem>

                                        <BitPivotItem HeaderText="@Localizer[nameof(AppStrings.Phone)]" Key="@PhoneKey">
                                            <BitTextField @bind-Value="model.PhoneNumber"
                                                          AutoFocus TabIndex="1"
                                                          Type="BitInputType.Tel"
                                                          Label="@Localizer[nameof(AppStrings.PhoneNumber)]"
                                                          IsEnabled="string.IsNullOrEmpty(PhoneNumberQueryString)"
                                                          Placeholder="@Localizer[nameof(AppStrings.PhoneNumberPlaceholder)]" />
                                            <ValidationMessage For="@(() => model.PhoneNumber)" />
                                        </BitPivotItem>
                                    </BitPivot>
                                }
                                else if (showEmail)
                                {
                                    <BitTextField @bind-Value="model.Email"
                                                  AutoFocus TabIndex="1"
                                                  Type="BitInputType.Email"
                                                  Label="@Localizer[nameof(AppStrings.Email)]"
                                                  IsEnabled="string.IsNullOrEmpty(EmailQueryString)"
                                                  Placeholder="@Localizer[nameof(AppStrings.EmailPlaceholder)]" />
                                    <ValidationMessage For="@(() => model.Email)" />
                                }
                                else if (showPhone)
                                {
                                    <BitTextField @bind-Value="model.PhoneNumber"
                                                  AutoFocus TabIndex="1"
                                                  Type="BitInputType.Tel"
                                                  Label="@Localizer[nameof(AppStrings.PhoneNumber)]"
                                                  IsEnabled="string.IsNullOrEmpty(PhoneNumberQueryString)"
                                                  Placeholder="@Localizer[nameof(AppStrings.PhoneNumberPlaceholder)]" />
                                    <ValidationMessage For="@(() => model.PhoneNumber)" />
                                }

                                <BitOtpInput @bind-Value="model.Token"
                                             Length="6"
                                             Size="BitSize.Large"
                                             Type="BitInputType.Number"
                                             OnFill="WrapHandled(HandleContinue)"
                                             Label="@Localizer[nameof(AppStrings.Token)]" />
                                <ValidationMessage For="@(() => model.Token)" />
                            </BitStack>

                            <BitButton ButtonType="BitButtonType.Button" OnClick="WrapHandled(HandleContinue)">
                                @Localizer[nameof(AppStrings.Continue)]
                            </BitButton>

                            @if (selectedKey == EmailKey)
                            {
                                <BitText Typography="BitTypography.Body1" Align="BitTextAlign.Center">
                                    @Localizer[nameof(AppStrings.NotReceivedEmailMessage)]
                                    <br />
                                    @Localizer[nameof(AppStrings.CheckSpamMailMessage)]
                                </BitText>
                            }
                            else
                            {
                                <BitText Typography="BitTypography.Body1" Align="BitTextAlign.Center">
                                    @Localizer[nameof(AppStrings.NotReceivedPhoneMessage)]
                                </BitText>
                            }

                            <BitButton AutoLoading
                                       Variant="BitVariant.Outline"
                                       OnClick="WrapHandled(Resend)"
                                       ButtonType="BitButtonType.Button">
                                @Localizer[nameof(AppStrings.Resend)]
                            </BitButton>
                        }
                        else
                        {
                            <BitStack FillContent>
                                <div style="min-height:80px">
                                    <BitTextField @bind-Value="model.Password"
                                                  AutoFocus TabIndex="1"
                                                  CanRevealPassword="true"
                                                  AutoComplete="new-password"
                                                  Type="BitInputType.Password"
                                                  Label="@Localizer[nameof(AppStrings.Password)]"
                                                  Placeholder="@Localizer[nameof(AppStrings.PasswordPlaceholder)]" />
                                    <ValidationMessage For="@(() => model.Password)" />
                                </div>
                                <div style="min-height:80px">
                                    <BitTextField @bind-Value="model.ConfirmPassword"
                                                  TabIndex="2"
                                                  CanRevealPassword="true"
                                                  AutoComplete="new-password"
                                                  Type="BitInputType.Password"
                                                  Label="@Localizer[nameof(AppStrings.ConfirmPassword)]"
                                                  Placeholder="@Localizer[nameof(AppStrings.ConfirmPassword)]" />
                                    <ValidationMessage For="@(() => model.ConfirmPassword)" />
                                </div>
                            </BitStack>

                            <BitButton IsLoading="isWaiting" ButtonType="BitButtonType.Submit">
                                @Localizer[nameof(AppStrings.ResetPasswordButtonText)]
                            </BitButton>
                        }
                    </BitStack>
                </EditForm>
            }
            else
            {
                <BitText Typography="BitTypography.H5">
                    @Localizer[nameof(AppStrings.ResetPasswordSuccessTitle), model.PhoneNumber!]
                </BitText>

                <BitText Typography="BitTypography.Subtitle1">
                    @Localizer[nameof(AppStrings.ResetPasswordSuccessBody)]
                </BitText>
            }
            <br />
            <BitStack Horizontal HorizontalAlign="BitAlignment.Center">
                <BitLink Href="@($"{PageUrls.SignIn}?return-url={Uri.EscapeDataString(ReturnUrlQueryString ?? PageUrls.Home)}")">@Localizer[nameof(AppStrings.SignIn)]</BitLink>
                <BitText>@Localizer[nameof(AppStrings.Or)]</BitText>
                <BitLink Href="@($"{PageUrls.SignUp}?return-url={Uri.EscapeDataString(ReturnUrlQueryString ?? PageUrls.Home)}")">@Localizer[nameof(AppStrings.SignUp)]</BitLink>
            </BitStack>

        </BitStack>
    </BitStack>
</section>
